package cn.com.bluemoon.daps.common.datascope.log.impl;

import cn.com.bluemoon.daps.common.datascope.log.ILogService;
import cn.com.bluemoon.daps.common.datascope.log.OperateLog;
import cn.com.bluemoon.daps.common.datascope.log.wrapper.ReaderBodyHttpServletRequestWrapper;
import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author Jarod.Kong
 */
@Slf4j
public class DefaultBeforeLogServiceImpl implements ILogService {
    @Override
    public String collector(OperateLog operateLog, HttpServletRequest request,
                            HttpServletResponse response,
                            UserInfoHolder.UserInfo userInfo) {

       /* String param = null;
        String requestURI = request.getRequestURI();
        try {
            ReaderBodyHttpServletRequestWrapper bodyReaderHttpServletRequestWrapper = new ReaderBodyHttpServletRequestWrapper(request);
            param = bodyReaderHttpServletRequestWrapper.getRequestPostStr(bodyReaderHttpServletRequestWrapper);
            Map<String, String[]> parameterMap = request.getParameterMap();
            if(StringUtils.isBlank(param) && !parameterMap.isEmpty()){
                Map<String, String> map = Maps.transformValues(parameterMap, arr -> StringUtils.join(arr));
                param = JSON.toJSONString(map);
            }
        }catch (IOException e){
            log.info(e.getMessage(), e);
        }*/
        String param = null;
        String requestURI = request.getRequestURI();
        Map<String, String[]> parameterMap = request.getParameterMap();
        if(!parameterMap.isEmpty()){
            Map<String, String> map = Maps.transformValues(parameterMap, arr -> StringUtils.join(arr));
            param = JSON.toJSONString(map);
        }
        return "url:" + requestURI + ";\n  参数:" + param;
    }
}
